package tk.mybatis.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.springboot.mapper.StudentMapper;
import tk.mybatis.springboot.mapper.TeacherMapper;
import tk.mybatis.springboot.mapper.UserMapper;
import tk.mybatis.springboot.model.BaseUser;
import tk.mybatis.springboot.model.Users;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2017/4/12.
 */
@Controller
@RequestMapping("/user")
@Transactional
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Users obj){
        return userMapper.insertSelective(obj);
    }
    @RequestMapping("")
    public String add(Model model){
        List<BaseUser> list = new ArrayList<>();
        list.addAll(teacherMapper.selectAll());
        list.addAll(studentMapper.selectAll());
        model.addAttribute("list",list);
        return "users/list";
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable String id){
        return userMapper.deleteByPrimaryKey(id);
    }



    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public String userDetail(Model model,@PathVariable Integer id
    ){
        BaseUser users = teacherMapper.selectByPrimaryKey(id);
        if(null == users){
            users = studentMapper.selectByPrimaryKey(id);
        }
        if(null == users){
            users = userMapper.selectByPrimaryKey(id
            );
        }
        model.addAttribute("i",users);
        return "detail";
    }

}
